package com.mass.core.framework.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.mass.core.token.JwtComponent;
import com.mass.core.utils.ResourcesFileUtils;

/**
 * <>
 *
 * @author liwenjun
 * @version 1.0.0
 * @date 2017/7/10 23:01
 */
@Component
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Value("${ueditor.upload-path}")
	private String path;
    
    @Value("${wx.context-path}")
   	private String wxPath;    //排除拦截
    
    @Autowired
    private JwtComponent jwtComponent;

    @Bean
    public PrivilegeInterceptor getPrivilegeInterceptor(){
    	return new PrivilegeInterceptor(jwtComponent);
    }
    
    
    /**
     * 配置拦截器
     * @author lance
     * @param registry
     */
    
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        InterceptorRegistration ir = registry.addInterceptor(getPrivilegeInterceptor());
        ir.excludePathPatterns("/sign/checkLogin");
        ir.excludePathPatterns("/sign/login");
        ir.excludePathPatterns("/service/**");
        ir.excludePathPatterns("/sign/getImageCodeMap");
        ir.excludePathPatterns("/config");
        ir.excludePathPatterns("/error");
        if(!ObjectUtils.isEmpty(wxPath) && !"false".equals(wxPath)){
        	if(wxPath.indexOf(",")>=0){
        		String [] list = wxPath.split(",");
        		for(String str:list){
        			ir.excludePathPatterns(str);
        		}
        	}else{
        		ir.excludePathPatterns(wxPath);
        	}
        }
        super.addInterceptors(registry);
        //ir.excludePathPatterns("/**/**");
        //ir.addPathPatterns("/sys/role/list");
    }
    
    /**
     * 配置静态文件
     * 如果我们将/xxxx/** 修改为 /** 与默认的相同时，则会覆盖系统的配置，可以多次使用 addResourceLocations 添加目录，
         * 优先级先添加的高于后添加的。
         *
         * 如果是/xxxx/** 引用静态资源 加不加/xxxx/ 均可，因为系统默认配置（/**）也会作用
         * 如果是/** 会覆盖默认配置，应用addResourceLocations添加所有会用到的静态资源地址，系统默认不会再起作用
     */
    
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    	 
    	 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
         registry.addResourceHandler("/fileURL/**").addResourceLocations("file:"+path.replaceAll("\\\\",""));
         super.addResourceHandlers(registry);
         //静态文件位置
         ResourcesFileUtils.path = path.replaceAll("\\\\","");
    }
}
